<?php
/* SVN FILE: $Id: edit.php 202 2009-09-04 06:27:25Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Edit Item page
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 202 $
* @lastrevision $Date: 2009-09-04 06:27:25 +0000 (Fri, 04 Sep 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-09-04 06:27:25 +0000 (Fri, 04 Sep 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/modules_admin/items/edit.php $
*/


if ( ! defined( 'IN_ACP' ) )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'admin.php'.";
	exit();
}

class admin_ibmarket_items_edit extends ipsCommand
{
	/**
	 * Skin object
	 *
	 * @access	private
	 * @var		object			Skin templates
	 */
	private $html;
	
	/**
	 * Main class entry point
	 *
	 * @access	public
	 * @param	object		ipsRegistry reference
	 * @return	void		[Outputs to screen]
	 */
	public function doExecute( ipsRegistry $registry )
	{
		if(!is_array($this->caches['market_items'][$this->request['item_id']]))
		{
			$this->registry->output->showError(null);
		}
		
		$this->registry->getClass('class_permissions')->checkPermissionAutoMsg( 'item_edit' );
		
		$this->html = $this->registry->output->loadTemplate('cp_skin_items');
		
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'admin_market', 'admin_items', 'admin_item_language' ) );
		
		$execute = array(
							'edit' => 'editForm',
							'update' => 'updateItem',
						);
		
		if(!isset($execute[$this->request['do']]))
		{
			$this->request['do'] = 'edit';
		}
		
		$function = $execute[$this->request['do']];
		$this->$function();
	}
	
	public function editForm()
	{
		$item = $this->caches['market_items'][$this->request['item_id']];
		
		$this->registry->output->html = $this->html->itemForm( marketRegistry::getItem($item), $item, 'edit', 'update', $this->lang->words['edit_item_title'], $this->lang->words['edit_item_button'] );
		
		$this->registry->getClass('output')->html_main .= $this->registry->getClass('output')->global_template->global_frame_wrapper();
		$this->registry->getClass('output')->sendOutput();
	}
	
	public function updateItem()
	{
		ipsRegistry::getClass('adminFunctions')->checkSecurityKey();
		
		if(!is_array($this->request['item']))
		{
			$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=items&amp;section=view" );
		}
		
		$input =& $this->request['item'];
		
		$item = marketRegistry::getItem($this->caches['market_items'][$this->request['item_id']]);
		
		$input['description'] = IPSText::getTextClass('bbcode')->preDbParse(IPSText::getTextClass('editor')->processRawPost('item_description'));
		
		$input['restock'] = intval($input['restock']);
		if($input['restock'] == 1 || $input['restock'] == 2)
		{
			$input['restock_when'] = numbers::max(0,numbers::parseInt($input['restock_when']));
		}
		elseif($input['restock'] == 3)
		{
			list($date, $time) = explode(" ",$input['restock_when']);
			list($month,$day,$year) = explode("/",$date);
			list($hour,$minute) = explode(":",$time);
			$input['restock_when'] = gmmktime( intval($hour), intval($minute), 0, intval($month), intval($day), intval($year) );
		}
		elseif($input['restock'] == 4)
		{
			$input['restock_when'] = 0;
		}
		else
		{
			$input['restock'] = 0;
			$input['restock_when'] = 0;
		}
		
		if(!is_array($this->caches['market_category'][$input['category']]))
		{
			$this->registry->output->showError( $this->lang->words['invalid_category'] );
		}
		
		$itemInfo = array(
							'filename' => $input['filename'],
							'item_name' => $input['item_name'],
							'item_description' => $input['description'],
							'category' => $input['category'],
							'icon' => $input['icon'],
							'hide_item' => intval($input['hide_item']) & 1,
							'base_price' => numbers::max(numbers::parseInt($input['base_price']),0),
							'base_resell' => numbers::max(numbers::parseInt($input['base_resell']),0),
							'allow_resell' => intval($input['allow_resell']) & 1,
							'resell_putback' => intval($input['resell_putback']) & 1,
							'tax' => numbers::parseFloat($input['tax']),
							'stock' => $input['stock'] == 'infinite' ? -1 : numbers::max(numbers::parseInt($input['stock']),-1),
							'restock' => $input['restock'],
							'restock_when' => $input['restock_when'],
							'restock_minute' => max(min(intval($input['restock_minute']), 60), 0),
							'restock_hour' => max(min(intval($input['restock_hour']), 24), 0),
							'restock_day' => max(min(intval($input['restock_day']), 7), 0),
							'restock_howmuch' => numbers::parseInt($input['restock_howmuch']),
							'inventory_limit' => numbers::parseInt($input['inventory_limit']),
							'limit_buy' => serialize(is_array($input['limit_buy']) ? $input['limit_buy'] : array()),
							'limit_use' => serialize(is_array($input['limit_use']) ? $input['limit_use'] : array()),
							'expire_time' => intval($time['expire_time']),
							'expire_unit' => intval($time['expire_unit']),
						);
		
		$itemInfo['restock_time'] = marketRegistry::nextRestockTime($itemInfo);
		
		$item->adminSave($itemInfo, $input);
		
		if(!isset($itemInfo['item_config']))
		{
			$itemInfo['item_config'] = '';
		}
		
		if(!isset($itemInfo['settings']))
		{
			$itemInfo['settings'] = '';
		}
		
		$this->DB->update('market_items',$itemInfo,"item_id=".$this->request['item_id']);
		
		$this->cache->rebuildCache('market_items','ibmarket');
		
		$this->lang->words['item_edit_log'] = str_replace("<#NAME#>",$input['item_name'],$this->lang->words['item_edit_log']);
		$this->lang->words['item_edit_log'] = str_replace("<#ID#>",$this->request['item_id'],$this->lang->words['item_edit_log']);
		ipsRegistry::getClass('adminFunctions')->saveAdminLog( $this->lang->words['item_edit_log'] );
		
		$this->registry->output->global_message .= $this->lang->words['item_edited'];
		$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=items&amp;section=view&amp;category=".$input['category'] );
	}
}